Skip to content

fix(message-history): fall back within the requested session in get_relevant() - #765

Open
vishal-bala wants to merge 1 commit into
mainfrom
fix/get-relevant-fallback-session-tag
Open

vishal-bala wants to merge 1 commit into
mainfrom
fix/get-relevant-fallback-session-tag

Conversation

@vishal-bala

@vishal-bala vishal-bala commented Oct 14, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

SemanticMessageHistory.get_relevant() accepts a session_tag and runs its vector search within that session. When nothing in the session is relevant and fall_back=True, it falls back to get_recent(), but without the session_tag. The fall-back therefore returns the recent messages of the instance's default session, so a lookup for one conversation can surface another conversation's messages. SemanticSessionManager inherits the same behaviour.

Changes

The fall-back now passes session_tag through to get_recent(), so it reads the same session the search did. When no session_tag is given, both still use the default session, as before. MessageHistory has no fall_back option and is unaffected.

A new integration test, test_semantic_get_relevant_falls_back_within_the_requested_session, stores one exchange in the default session and one in a second session. It then calls get_relevant() for the second session with distance_threshold=0, which forces the fall-back. Against main the test fails, returning the default session's exchange; with this change it returns the requested session's.

Release Notes

SemanticMessageHistory.get_relevant() and SemanticSessionManager.get_relevant() called with both session_tag and fall_back=True now fall back to the recent messages of that session. Previously they returned the recent messages of the instance's default session.


Note

Medium Risk
Changes multi-session context retrieval for semantic history fallback, which could affect LLM prompt assembly but is a targeted bugfix with a regression test.

Overview
Fixes a session leak when SemanticMessageHistory.get_relevant() (and SemanticSessionManager, which subclasses it) runs with both session_tag and fall_back=True. Vector search was already scoped to that session, but the empty-result fallback called get_recent() without session_tag, so recent messages from the instance default session could appear in another conversation’s context.

The fallback now forwards session_tag (and existing role / formatting args) to get_recent(), so recent context stays in the same session; behavior when session_tag is omitted is unchanged.

Adds integration test test_semantic_get_relevant_falls_back_within_the_requested_session (two sessions, forced fallback via distance_threshold=0) to lock in session-scoped fallback.

Reviewed by Cursor Bugbot for commit 3d972cc. Bugbot is set up for automated code reviews on this repo. Configure here.

…elevant()

get_relevant(session_tag=..., fall_back=True) searched the requested
session but, when nothing was relevant, fell back to get_recent()
without the session_tag. It therefore returned the recent messages of
the instance's default session, letting a lookup for one conversation
surface another's. The fall-back now passes session_tag through, so it
reads the same session the search did. SemanticSessionManager inherits
the fix; MessageHistory has no fall_back and is unaffected.

The new integration test stores one exchange in the default session and
one in another, then asks for the other session with distance_threshold=0
so the fall-back fires. On main it returns the default session's
exchange; with this change it returns the requested one.
@vishal-bala vishal-bala added the auto:patch Increment the patch version when merged label Oct 14, 2026
@vishal-bala
vishal-bala marked this pull request as ready for review October 15, 2026 08:39

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

auto:patch Increment the patch version when merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant